require('./page.css');
const cp = require('cp');
let moduleExport = {};
let data = {
  org: {
    id: '',
    title: '',
    address: '',
    brief: '',
  },
  contact: {
    id: '',
    realname: '',
    mobile: '',
    email: '',
    address: '',
    idcardno: '',
  },
  credit: {
    type: 1,
  },
  obj_edit: {
    org: 'enter',
    contact: 'leave',
    credit: 'leave',
  },
};
avalon.effect('animate', {
  enterClass: 'animate-enter',
  enterActiveClass: 'animate-enter-active',
  leaveClass: 'animate-leave',
  leaveActiveClass: 'animate-leave-active',
});
avalon.effect('contact', {
  enterClass: 'contact-enter',
  enterActiveClass: 'contact-enter-active',
  leaveClass: 'contact-leave',
  leaveActiveClass: 'contact-leave-active',
});
avalon.effect('credit', {
  enterClass: 'credit-enter',
  enterActiveClass: 'credit-enter-active',
  leaveClass: 'credit-leave',
  leaveActiveClass: 'credit-leave-active',
});
avalon.validators.identity = {
  // 自定义身份证验证方法
  message: '请输入合法的身份证号',
  get: (value, field, next) => {
    let pattern = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(value);
    next(pattern);
    return value;
  },
};

let vm = avalon.define({
  $id: 'mc_stpe1_edit',
  data: data,
  vali_org: {
    onValidateAll: reasons => {
      if (reasons.length) {
        cp.layerMsg(reasons[0].getMessage());
      } else {
        cp.ajaxUrl({
          controller: 'shopskt',
          cmd: 'com.rel.porg.pb.shop_update_org_req',
          data: {
            info: vm.data.org,
          },
          success: res => {
            vm.data.obj_edit = {
              org: 'leave',
              contact: 'enter',
              credit: 'leave',
            };
          },
          fail: res => {
            cp.layerMsg(res.desc);
          },
        });
      }
    },
  },
  vali_contact: {
    onValidateAll: reasons => {
      if (reasons.length) {
        cp.layerMsg(reasons[0].getMessage());
      } else {
        cp.ajaxUrl({
          controller: 'shopskt',
          cmd: 'com.rel.porg.pb.shop_update_acc_req',
          data: {
            info: vm.data.contact,
          },
          success: res => {
            vm.data.obj_edit = {
              org: 'leave',
              contact: 'leave',
              credit: 'enter',
            };
          },
          fail: res => {
            cp.layerMsg(res.desc);
          },
        });
      }
    },
  },
  vali_credit: {
    onValidateAll: reasons => {
      if (reasons.length) {
        cp.layerMsg(reasons[0].getMessage());
      } else {
        cp.layerConfirm({
          msg: '姓名和身份证件号必须匹配，实名认证信息不可更改',
          yes: (index, layer) => {
            layer.close(index);
            let newtab = window.open('about:blank');
            cp.ajaxUrl({
              controller: 'shopskt',
              cmd: 'com.rel.porg.pb.shop_update_acc_req',
              data: {
                info: {
                  idcardno: vm.data.contact.idcardno,
                  id: vm.data.contact.id,
                },
              },
              success: res => {
                let url = cp.API_ROOT + 'zhima/grantPc?token=' +
                cp.getCookie('token') + '&mobile=' +
                vm.data.contact.mobile + '&idcard=' + vm.data.contact.idcardno;
                newtab.location = url;
                cp.layerConfirm({
                  msg: '我已授权芝麻信用',
                  btn: ['确认', '放弃'],
                  yes: (index, layer) => {
                    avalon.router.navigate('/myshop/apply/step1/view', 2);
                    layer.close(index);
                  },
                });
              },
              fail: res => {
                cp.layerMsg(res.desc);
              },
            });
          },
        });
      }
    },
  },
  bindEdit: type => {
    for (let i in vm.data.obj_edit) {
      if (i === type) {
        vm.data.obj_edit[i] = 'enter';
      } else {
        vm.data.obj_edit[i] = 'leave';
      }
    }
  },
  init: () => {
    cp.ajaxUrl({
      // 获取公司初始信息
      controller: 'shopskt',
      cmd: 'com.rel.porg.pb.com_sel_org_req',
      data: {},
      success: res => {
        for (let i in vm.data.org) {
          if (res.org[i]) {
            vm.data.org[i] = res.org[i];
          }
        }
      },
      fail: res => {
        cp.layerMsg(res.desc);
      },
    });
    cp.ajaxUrl({
      // 获取用户初始信息
      controller: 'shopskt',
      cmd: 'com.rel.porg.pb.com_sel_user_req',
      data: {},
      success: res => {
        for (let i in vm.data.contact) {
          if (res.user[i]) {
            vm.data.contact[i] = res.user[i];
          }
        }
      },
      fail: res => {
        cp.layerMsg(res.desc);
      },
    });
  },
});

vm.init();
moduleExport.vm = vm;
moduleExport.content = require('./content.html');
module.exports = moduleExport;
